#! /usr/local/bin/python

# Web 2.0
# A simple CGI script to retrieve a blog and return an HTML page
# 
# Note: fill in your own username and passwd!

import cgi
import urllib
import urllib2

username = '51158405'
passwd   = 'gxlty725'















# get form variables (if desired)
form = cgi.FieldStorage()
# an example below
if 'title' in form:
  title = form["title"].value
else:
  title = "no title!"
if 'content' in form:
  content = form["content"].value
else:
  content = "no content!"

# your username and password for the blog service
blogurl  = 'http://www.cs.cityu.edu.hk/~cs4380/webservice/blog/createPost.py'

# setup the password
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, 'http://www.cs.cityu.edu.hk/', username, passwd)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

# setup the parameters for calling the web service
paramslist = {
	'author': username, 
        'title': title,
        'content': content,
}
params = urllib.urlencode(paramslist)

# call the webservice
data = urllib2.urlopen(blogurl, params).read()

# print out the content header
print 'Content-type: text/html\n\n'

print '<html><body>\n'
print '<h1>Post successful</h1>\n'
print 'The UUID of your new post is: '
print data
print '<p><a href=index.html>back to form</a>\n'
print '</body></html>\n'


